class BluetoothService {
  constructor() {
    // 使用小写的 UUID
    this.SERVICE_UUID = '0000ffe0-0000-1000-8000-00805f9b34fb'
    this.CONTROL_CHARACTERISTIC_UUID = '0000ffe1-0000-1000-8000-00805f9b34fb'
    this.NOTIFY_CHARACTERISTIC_UUID = '0000ffe2-0000-1000-8000-00805f9b34fb'
    
    this.device = null
    this.server = null
    this.service = null
    this.controlCharacteristic = null
    this.notifyCharacteristic = null
    this.notificationCallback = null
  }

  // 检查浏览器是否支持 Web Bluetooth
  checkAvailability() {
    if (!navigator.bluetooth) {
      throw new Error('此浏览器不支持 Web Bluetooth')
    }
  }

  // 连接设备
  async connect() {
    try {
      // 请求选择设备，使用更宽松的过滤条件
      this.device = await navigator.bluetooth.requestDevice({
        // 使用可选过滤器
        filters: [
          {
            name: 'BlueRecord'
          }
        ],
        // 明确指定要使用的服务 UUID
        optionalServices: [this.SERVICE_UUID]
      })

      console.log('选择的设备:', this.device)

      // 监听设备断开连接
      this.device.addEventListener('gattserverdisconnected', this.onDisconnected.bind(this))

      // 连接到设备
      console.log('正在连接到 GATT 服务器...')
      this.server = await this.device.gatt.connect()

      // 获取服务
      console.log('正在获取服务...')
      this.service = await this.server.getPrimaryService(this.SERVICE_UUID)

      // 获取特征值
      console.log('正在获取特征值...')
      this.controlCharacteristic = await this.service.getCharacteristic(this.CONTROL_CHARACTERISTIC_UUID)
      this.notifyCharacteristic = await this.service.getCharacteristic(this.NOTIFY_CHARACTERISTIC_UUID)

      // 启用通知
      console.log('正在启用通知...')
      await this.notifyCharacteristic.startNotifications()
      this.notifyCharacteristic.addEventListener('characteristicvaluechanged', this.onNotificationReceived.bind(this))

      console.log('蓝牙连接成功')
      return true
    } catch (error) {
      console.error('连接失败:', error)
      throw error
    }
  }

  // ... 其他代码保持不变 ...
} 